package com.tyf.common.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.tyf.common.constant.DateFormats;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * 日期时间工具类
 * 
 */
public class DateUtils {
	private static Logger logger = LoggerFactory.getLogger(DateUtils.class);
	
    public String formatDate(java.util.Date date) {
        return formatDateByFormat(date, DateFormats.DF1_8);
    }

    public static String formatDateByFormat(java.util.Date date, String format) {
        String result = "";
        if (date != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                result = sdf.format(date);
            } catch (Exception ex) {
            	logger.error("",ex);
            }
        }
        return result;
    }
    
    public static java.util.Date parseDate(java.sql.Date date) {
        return date;
    }

    public static java.sql.Date parseSqlDate(java.util.Date date) {
        if (date != null) {
            return new java.sql.Date(date.getTime());
        } else {
            return null;
        }
    }

    public static String format(java.util.Date date, String format) {
        String result = "";
        try {
            if (date != null) {
                java.text.DateFormat df = new java.text.SimpleDateFormat(format);
                result = df.format(date);
            }
        } catch (Exception e) {
        	logger.error("",e);
        }
        return result;
    }

    public static String format(java.util.Date date) {
        return format(date, DateFormats.DF2_8);
    }

    public static String format1(java.util.Date date) {
        return format(date, DateFormats.DF1_8);
    }

    public static int getYear(java.util.Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        return c.get(java.util.Calendar.YEAR);
    }

    public static int getMonth(java.util.Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        return c.get(java.util.Calendar.MONTH) + 1;
    }

    public static int getDay(java.util.Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        return c.get(java.util.Calendar.DAY_OF_MONTH);
    }

    public static int getHour(java.util.Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        return c.get(java.util.Calendar.HOUR_OF_DAY);
    }

    public static int getMinute(java.util.Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        return c.get(java.util.Calendar.MINUTE);
    }

    public static int getSecond(java.util.Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        return c.get(java.util.Calendar.SECOND);
    }

    public static long getMillis(java.util.Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        return c.getTimeInMillis();
    }

    public static int getWeek(java.util.Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        int dayOfWeek = c.get(java.util.Calendar.DAY_OF_WEEK);
        dayOfWeek = dayOfWeek - 1;
        if (dayOfWeek == 0) {
            dayOfWeek = 7;
        }
        return dayOfWeek;
    }

    public static String getDate(java.util.Date date) {
        return format(date, DateFormats.DF2_8);
    }

    public static String getDate(java.util.Date date,String formatStr) {
        return format(date, formatStr);
    }


    public static String getTime(java.util.Date date) {
        return format(date, "HH:mm:ss");
    }

    public static String getDateTime(java.util.Date date) {
        return format(date, DateFormats.DF1_16);
    }
    
    
    /**
     * 日期相加
     * @param date Date
     * @param day int
     * @return Date
     */
    public static java.util.Date addDate(java.util.Date date, int day) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
        return c.getTime();
    }

    /**
     * 日期相减
     * @param date Date
     * @param date1 Date
     * @return int
     */
    public static int diffDate(java.util.Date date, java.util.Date date1) {
        return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000));
    }
    
    /**
     * 日期相减(返回秒值)
     * @param date Date
     * @param date1 Date
     * @return int
     * @author 
     */
    public static Long diffDateTime(java.util.Date date, java.util.Date date1) {
        return (Long) ((getMillis(date) - getMillis(date1))/1000);
    }

    public static java.util.Date getdate(String date) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(DateFormats.DF1_8);
        return sdf.parse(date);
    }

    public static java.util.Date getdate1(String date) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(DateFormats.DF1_16);
        return sdf.parse(date);
    }
    
    public static java.util.Date getdate(String date,String formatStr) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        return sdf.parse(date);
    }
    
    public static java.util.Date getMaxTimeByStringDate(String date) throws Exception {
    	String maxTime = date + " 23:59:59";
    	SimpleDateFormat sdf = new SimpleDateFormat(DateFormats.DF1_16);
    	return sdf.parse(maxTime);
    }
    /**
     * 获得当前时间
     * @return
     */
    public static Date getCurrentDateTime()
    {
    	Date date=new Date();
    	SimpleDateFormat sdf = new SimpleDateFormat(DateFormats.DF1_16);    	
        String result = DateUtils.getDateTime(date);
    	try {
			return sdf.parse(result);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			logger.error("",e);
		}
		return null;
    	
    }
    
    public static String getCurrentDateTimeToStr() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(getCurrentDateTime());
	}
    public static String getCurrentDateTimeToStr2() {
		SimpleDateFormat sdf = new SimpleDateFormat(DateFormats.DF1_16);
		return sdf.format(getCurrentDateTime());
	}

    public static Long getWmsupdateDateTime() {
		Calendar cl=Calendar.getInstance();
		
		return cl.getTimeInMillis();
	}
    
    public static Integer getLeftSeconds(String date)throws Exception{
    	SimpleDateFormat sdf = new SimpleDateFormat(DateFormats.DF1_16);
    	Date condition = sdf.parse(date);
    	long n = condition.getTime();
    	long s = sdf.parse(getCurrentDateTimeToStr2()).getTime();
//    	System.out.println("开始时间:"+date+"-->"+(int)((s-n)/1000));
    	return (int)((s-n)/1000);
    }
    
    /**
     * 获得时间戳
     * @return
     * @throws Exception
     */
    public static String getTime(){
    	Date date=new Date();
    	return String.valueOf(date.getTime());
    }
    
    public static int isDate(Date dt1, Date dt2){
    	int  r = 0;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
			
			Date time1= sdf.parse(sdf.format(dt1));
			Date time2= sdf.parse(sdf.format(dt2));
//			System.out.println(time1);
//			System.out.println(time2);
			if (time1.equals(time2)) {
				r = 0;
			} else if (time1.before(time2)) {
				r = -1;
			} else {
				r = 1;
			}
		} catch (Exception ex) {
			 //ex.printStackTrace();
			logger.error("",ex);
	    }
		return r;
	}
    
    public static void main(String[] args) {
        try {
        	System.out.println(getLeftSeconds("2011-12-29 23:33:09"));
        	System.out.println(Math.abs(-1110));
        } catch (Exception ex) {
            //ex.printStackTrace();
        	logger.error("",ex);
        }
    }
    
	public static Date getDateByDays(String days,String format) throws Exception{            
       
                    if(days!=null){
                            SimpleDateFormat sdf=new SimpleDateFormat(format);
                            int d=Integer.valueOf(days)-2;
                            if(d>78760){
                                    return null;
                            }
                            if(format.equals(DateFormats.DF1_8)){
                            	return DateUtils.addDate(sdf.parse("1900-01-01"), d);
                            }else{
                            	return DateUtils.addDate(sdf.parse("1900-01-01 00:00:00"), d);
                            	
                            }
                    }
          
        return null;
        
        
}
}
